﻿using DevExpress.Xpo;
using DevExpress.Xpo.DB;
using DevExpress.Xpo.Metadata;
using System;

namespace 干涉检查Demo
{
    /// <summary>
    /// 连接数据库基础信息
    /// </summary>
    public class XpoCon
    {
        public static IDataStore store;//创建数据存储对象    
        private static readonly object lockObj = new object();
        private static ThreadSafeDataLayer dataLayer = null;//全局线程安全数据层 
        private static UnitOfWork shortSession = null;//短会话单元 
        private static UnitOfWork longtSession = null;//长连接会话


        /// <summary>
        /// 全局持久会话
        /// </summary>
        public static UnitOfWork UnitOfLong
        {
            get
            {
                if (longtSession == null)
                {
                    lock (lockObj)
                    {
                        if (longtSession == null)
                        {
                            ThreadSafeDataLayer dataLayer = SafeDataLayer();
                            longtSession = new UnitOfWork(dataLayer)
                            {
                                TrackPropertiesModifications = true
                            };
                        }
                    }
                }
                return longtSession;
            }
        }

    


        /// <summary>
        /// 全局短会话
        /// </summary>
        public static UnitOfWork UnitOfShort
        {
            get
            { 
                if (shortSession == null)
                { 
                    lock (lockObj)
                    {
                        if (shortSession == null)
                        {
                            ThreadSafeDataLayer dataLayer = SafeDataLayer();
                            shortSession = new UnitOfWork(dataLayer)
                            {
                                TrackPropertiesModifications = true
                            };
                       
                        }
                    }
                } 
                return shortSession;
            }
        } 

        /// <summary>
        /// 释放短UnitOfWork
        /// </summary>
        public static void ReleaseShortUnitOfWork()
        {
            if (shortSession != null)//释放短绘画单元
            {
                shortSession?.Connection?.Close();
                shortSession?.Dispose();
                shortSession = null; 
            } 
        }

        /// <summary>
        /// 释放长UnitOfWork
        /// </summary>
        public static void ReleaseLongUnitOfWork()
        {
            if (longtSession != null)//释放长会话单元
            {
                longtSession?.Connection?.Close();
                longtSession?.Dispose();
                longtSession = null;
            }
        }


        /// <summary>
        /// 释放长DataLayerk
        /// </summary>
        public static void ReleaseDataLayer()
        {
            if (dataLayer != null)//释放长会话单元
            {
                dataLayer?.Connection?.Close();
                dataLayer?.Dispose();
                dataLayer = null;
            }
        }


        /// 注册基于线程安全的数据层
        /// </summary>
        /// <returns></returns>
        public static ThreadSafeDataLayer SafeDataLayer()
        {
            try
            { 
                if (dataLayer == null )
                {
                    XPDictionary dict = new ReflectionDictionary(); //创建数据层 
                    //dict.CollectClassInfos(typeof(Program).Assembly);
                    dict.CollectClassInfos(System.Reflection.Assembly.GetExecutingAssembly());
                   // dict.GetDataStoreSchema(typeof(Program).Assembly);
                    dict.GetDataStoreSchema(System.Reflection.Assembly.GetExecutingAssembly());
                    dataLayer = new ThreadSafeDataLayer(dict, store);
                  
                } 
                return dataLayer;
            }
            catch (Exception ex)
            {
                Msg.ErrorMessageShow(ex.Message);
                return null;
            } 

        }
    }
}
